package com.shanhesoft.java.modules.exam.web;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.rancher.type.Container;
import com.shanhesoft.java.modules.exam.service.impl.RancherService;
import com.shanhesoft.java.modules.sys.domain.dto.ContainerDTO;
import com.shanhesoft.java.modules.sys.domain.vo.ContainerVolumeVO;

@RestController
@RequestMapping("/api/rancher")
public class RancherApiResource {
	
	@Autowired
	private RancherService rancherService;
	
	@RequestMapping("/createContainer")
	public Result<Container> createContainer(@RequestBody ContainerDTO container) {
		Result<Container> containerResult = rancherService.createContainer(container);
		return containerResult;
	}
	
	@RequestMapping("/stopContainer")
	public Result<Container> stopContainer(String containerId) {
		Result<Container> stopContainer = rancherService.stopContainer(containerId);
		return stopContainer;
	}
	
	@RequestMapping("/stopContainers")
	public Result<String> stopContainers(String containerIds) {
		if(!StringUtils.isEmpty(containerIds)) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					String[] split = containerIds.split(",");
					for (String id : split) {
						rancherService.stopContainer(id);
					}
				}
			}).start();
		}
		return Result.buildOkData(containerIds, "停止任务已提交后台执行！");
	}
	
	@RequestMapping("/startContainer")
	public Result<Container> startContainer(String containerId, Integer runTime) {
		Result<Container> stopContainer = rancherService.startContainer(containerId, runTime);
		return stopContainer;
	}

	@RequestMapping("/deleteContainer")
	public Result deleteContainer(String containerId){
		Result resp = rancherService.deleteContainer(containerId);
		return resp;
	}
	
	@RequestMapping("/deleteContainerByids")
	public Result<String> deleteContainerByids(String containerIds){
		if(!StringUtils.isEmpty(containerIds)) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					String[] split = containerIds.split(",");
					for (String id : split) {
						rancherService.deleteContainer(id);
					}
				}
			}).start();
		}
		return Result.buildOkData(containerIds, "删除任务已提交后台执行！");
	}
	
	@RequestMapping("/deleteContainers")
	public Result deleteContainers(){
		rancherService.deleteContainers();
		return Result.buildOk("删除全部容器及存储卷任务已提交！");
	}
	
//	@RequestMapping("/delayed")
//	public R<String> delayed(String containerId, Integer time) {
//		if(StringUtils.isEmpty(containerId)) {
//			return R.data(R.ERROR, "参数containerId不能为空！", 1060, "0000-00-00 00:00:00");
//		}
//		if(time == null || time.intValue() > 60 || time.intValue() <= 0) {
//			time = 30;
//		}
//		Long delayed = time.longValue()*60*1000;
//		Long delayedTime = containerRunTimeService.delayed(containerId, delayed);
//		if(delayedTime.longValue() == 0L) {
//			return R.data(R.ERROR, "延时失败！容器已停止！", 1061, "0000-00-00 00:00:00");
//		}
//		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		String format = dateFormat.format(new Date(delayedTime));
//		return R.data(R.SUCCESS, "延时成功！", 1062, format);
//	}
	
//	@RequestMapping("/getContainerStopTime")
//	public R<Long> getContainerStopTime(String containerId){
//		List<ContainerRunTime> list = containerRunTimeService.findByContainerId(containerId);
//		if(list != null && list.size() > 0) {
//			Long endTime = list.get(0).getEndTime();
//			return R.data(R.SUCCESS, "查询成功", 1102, endTime);
//		}else {
//			return R.data(R.ERROR, "未查询到容器的停止时间", 1101, 0L);
//		}
//	}

	@RequestMapping("/listAllUserContainers")
	public Result<List<Container>> listAllUserContainers(){
		return rancherService.listAllUserContainers();
	}
	
	@RequestMapping("/listConatinersVolumes")
	public Result<List<ContainerVolumeVO>> listConatinersVolumes(){
		return rancherService.listConatinersVolumes();
	}
	
	@RequestMapping("/isContainerExist")
	public Result<Boolean> isContainerExist(String contanerName){
		return rancherService.isContainerExist(contanerName);
	}
	
	@RequestMapping("/listAllUserContainersByCustomer")
	public Result<List<Container>> listAllUserContainers(HttpServletRequest request){
		Enumeration<String> parameterNames = request.getParameterNames();
		Map<String, Object> filters = new HashMap<String, Object>();
		while (parameterNames.hasMoreElements()) {
			String element = parameterNames.nextElement();
			filters.put(element, request.getParameter(element));
		}
		return rancherService.listAllUserContainers(filters);
	}
}
